<template>
  <div class="main">
    <div class="password">
      <!-- 展示个人头像和昵称 -->
      <div class="demo-left">
        <div class="touxiang">
          <img :src="avatarUrl" alt="">
          <span class="span_username">{{ username }}</span>
        </div>
      </div>
      <!-- 修改密码 -->
      <el-form ref="ruleForm2" :model="ruleForm2" :rules="rules2" label-width="120px;" class="demo-right">
        <el-form-item label="旧密码" prop="oldPass">
          <el-input
            :type="pwdOldType"
            v-model="ruleForm2.oldPass"
            placeholder="请输入你的旧密码"/>
          <span class="show-pwd" @click="showOldPwd">
            <svg-icon icon-class="eye" />
          </span>
        </el-form-item>
        <el-form-item label="新密码" prop="newPass">
          <el-input v-model="ruleForm2.newPass" :type="pwdNewType" placeholder="请输入你的新密码"/>
          <span class="show-pwd" @click="showNewPwd">
            <svg-icon icon-class="eye" />
          </span>
        </el-form-item>
        <el-form-item label="确认新密码" prop="checkPass">
          <el-input v-model="ruleForm2.checkPass" :type="pwdCheckType" placeholder="请重新输入你的新密码"/>
          <span class="show-pwd" @click="showCheckPwd">
            <svg-icon icon-class="eye" />
          </span>
        </el-form-item>
        <el-form-item>
          <el-button :loading="addLoading" type="primary" @click.native="submitForm('ruleForm2')">确认</el-button>
          <el-button @click.native="resetForm('ruleForm2')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { isvalidatePass } from '@/utils/validate'
import { editPass } from '@/api/home'
export default {
  name: 'Password',
  data() {
    // 确认新密码
    const checkPass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请重新输入你的新密码'))
      } else if (value !== this.ruleForm2.newPass) {
        callback(new Error('两次输入密码不一致'))
      } else {
        callback()
      }
    }
    // 新密码
    const validateNewPass = (rule, value, callback) => {
      if (!isvalidatePass(value)) {
        callback(new Error('密码至少由数字、大写、小写字母其中两种组成，且长度不小于6，同时第一位不能为数字'))
      } else if (value === '') {
        callback(new Error('请输入你的新密码'))
      } else if (value === this.ruleForm2.oldPass) {
        callback(new Error('新密码不能和上一次的旧密码一样，请重新输入'))
      } else {
        callback()
      }
    }
    // 旧密码
    const validateOldPass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入你的旧密码'))
      } else if (value === this.ruleForm2.newPass) {
        callback(new Error('旧密码不能和新密码一样'))
      } else {
        callback()
      }
    }
    return {
      pwdOldType: 'password',
      pwdNewType: 'password',
      pwdCheckType: 'password',
      addLoading: false,
      ruleForm2: {
        oldPass: '',
        newPass: '',
        checkPass: ''
      },
      rules2: {
        oldPass: [
          { validator: validateOldPass, trigger: 'blur' }
        ],
        newPass: [
          { validator: validateNewPass, trigger: 'blur' }
        ],
        checkPass: [
          { validator: checkPass, trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    ...mapGetters([
      'password',
      'username',
      'avatarUrl'
    ])
  },
  methods: {
    // 展示旧、新、确认新密码
    showOldPwd() {
      if (this.pwdOldType === 'password') {
        this.pwdOldType = ''
      } else {
        this.pwdOldType = 'password'
      }
    },
    showNewPwd() {
      if (this.pwdNewType === 'password') {
        this.pwdNewType = ''
      } else {
        this.pwdNewType = 'password'
      }
    },
    showCheckPwd() {
      if (this.pwdCheckType === 'password') {
        this.pwdCheckType = ''
      } else {
        this.pwdCheckType = 'password'
      }
    },
    // 提交确认修改密码
    submitForm(ruleForm2) {
      this.$refs[ruleForm2].validate((valid) => {
        this.$confirm('确认提交吗？', '提示', {}).then(() => {
          this.addLoading = true
          const para = Object.assign({}, this.ruleForm2)
          editPass(para).then((res) => {
            console.log('editpass')
            this.addLoading = false
            this.$message({
              message: '提交成功',
              type: 'success'
            })
            this.$refs['ruleForm2'].resetFields()
          })
        })
      })
    },
    // 重置按钮
    resetForm(formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>

<style lang="scss" scoped>
$dark_gray:#889aa4;
.main {
  width: 100%;
  height: 1000px;
  background-color: white
}
.password{
  width: 80%;
  height: 100%;
  margin: 0 auto;
  padding-top: 50px;
  // margin-top: 50px;
  background-color: white
}
.demo-left{
  width: 30%;
  float: left;
}
.touxiang{
  width: 100%
}
img{
  width: 60%;
  height: 60%;
  margin: 10% 20%
}
.span_username{
  display: block;
  margin: 0 auto;
  text-align: center
}
.demo-right{
  width: 68%;
  float: right;
}
.show-pwd {
  position: absolute;
  right: 10px;
  top: 42px;
  font-size: 16px;
  color: $dark_gray;
  cursor: pointer;
  user-select: none;
}
.svg-icon{
  fill: black
}
</style>
